package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.domain.vo.EchartsVo;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhm
 * @since 2023-03-23
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;


    @Override
    public void settlement(Shop shop) {
        //参数校验
        if(StringUtils.isEmpty(shop.getName())){
            throw new CustomException("店铺名称不合法!!");
        }
        //校验密码是否一致
        if(shop.getAdmin().getPassword() == null || shop.getAdmin().getComfirmPassword() == null){
            throw new CustomException("密码不合法!!");
        }

        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new CustomException("密码不一致!!");
        }
        shop.setState(0);//待审核状态
        shop.setRegisterTime(new Date());
        //校验店铺是否已经存在
        Shop oldShop = shopMapper.loadByName(shop.getName());
        if(oldShop != null){
            throw new CustomException("店铺已经存在!");
        }
        //TODO 审核!!!!百度AI来做
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new CustomException("店铺名称不符合核心价值观");
        }
        //校验员工是否已经存在
        Employee emp = employeeMapper.loadByUsername(shop.getAdmin().getUsername());
        //存在
        if(emp != null){
            //校验该员工是否已经有店铺了
            if(emp.getShop_id() != null){
                throw new CustomException("该员工不可用!");
            }else{
                //如果员工存在并且没有店铺id,保存店铺
                shop.setAdminId(emp.getId());
                shopMapper.add(shop);
                //给员工更新店铺id
                emp.setShop_id(shop.getId());
                employeeMapper.update(emp);
            }
        }
        //先保存员工
        shop.getAdmin().setState(0);//设置员工初始状态
        employeeMapper.add(shop.getAdmin());
        //给店铺设置员工自增id，保存店铺
        shop.setAdminId(shop.getAdmin().getId());
        shopMapper.add(shop);
        //拿到店铺自增id更新后员工的店铺id
        shop.getAdmin().setShop_id(shop.getId());
        employeeMapper.update(shop.getAdmin());
    }

    @Override
    @Transactional
    public void auditPass(ShopAuditLog shopAuditLog) {
        //更新店铺审核状态
        Shop shop = shopMapper.queryById(shopAuditLog.getShopId());
        if(shop == null){
            throw new CustomException("数据异常");
        }
        shop.setState(1);//审核通过
        shopMapper.update(shop);
        //保存审核日志
        shopAuditLog.setAuditId(334L);
        shopAuditLog.setAuditTime(new Date());
        shopAuditLogMapper.add(shopAuditLog);
        //发送激活邮件 把员工的状态改成1可用  激活连接是访问后台接口的一个地址  http://localhost:8080/shop/active?shopid=32
        //创建简单右键消息
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("hm3761176@vip.qq.com");
        simpleMailMessage.setTo("hm3761176@vip.qq.com");
        simpleMailMessage.setSubject("今天天气真不错！");
        simpleMailMessage.setText("这里是激活员工账号的地址,其实就是对应我们的后端controller接口");

        javaMailSender.send(simpleMailMessage);

    }

    @Override
    public HashMap<String, List<Integer>> storeStatistics() {
        //结果集map
        HashMap<String, List<Integer>> hm = new HashMap<>();
        //查询统计结果
        List<EchartsVo> echartsVos = shopMapper.storeStatistics();
        
        //初始化x轴集合 和 y轴集合
        ArrayList<Integer> x = new ArrayList<>();
        ArrayList<Integer> y = new ArrayList<>();

        for (EchartsVo vo : echartsVos) {
            x.add(vo.getStates());
            y.add(vo.getCounts());
        }

        hm.put("x",x);
        hm.put("y",y);
        return hm;
    }


}
